/**
 * 分享
 */
var shares = {};
// H5 plus事件处理
function plusReady(){
	updateSerivces();
	if(plus.os.name=="Android"){
		main = plus.android.runtimeMainActivity();
		Intent = plus.android.importClass("android.content.Intent");
		File = plus.android.importClass("java.io.File");
		Uri = plus.android.importClass("android.net.Uri");
	}
}
if(window.plus){
	plusReady();
}else{
	document.addEventListener("plusready",plusReady,false);
}

/**
 * 更新分享服务
 */
function updateSerivces(){
	//获取分享通道
	plus.share.getServices(function(data) {
		for(var i in data) {
			var index = data[i];
			shares[index.id] = index;
		}
	}, function(e) {
		toast("获取分享服务列表失败：" + e.message);
	})
}



// 分享操作 
function shareAction(id , ex , aid , title , body , picture , uid , tid ){
	plus.nativeUI.showWaiting('操作中...');
	var sname = null;
	if(!id || !(sname = shares[id])){
		toast('无效的分享服务');
	}
	//验证是否授权
	if(sname.authenticated){
		send(sname , ex , aid , title , body , picture , uid , tid);
	}else{
		sname.authorize(function(){
			send(sname , ex , aid , title , body , picture , uid , tid);
		} , function(e){
			plus.nativeUI.closeWaiting();
			toast('授权失败:代码' + e.code + '内容:' + e.message)
		})
	}
}

function send(obj , ex , aid , title , body , picture , uid , tid){
	obj.send({
		title: title,
		extra: {
			scene: ex
		},
		content: body,
		href: apiRoot + '/Home/Share/share/aid/'+aid+'/tid/'+tid, 
		thumbs: ['images/me-png']
	} , function(){
		plus.nativeUI.closeWaiting();
		$.ajax({
			type:"post", 
			url:apiRoot + '/Home/Share/shareIntegral/',
			data:{uid : uid , aid : aid , tid : tid},
			success : function(data){}
		});
		toast('分享成功');
	} , function(e){
		plus.nativeUI.closeWaiting();
		toast('您已取消分享');
	});
}


//// 分享操作 
//function shareAction(id, ex , aid , title , body , picture) {
//	console.log(ex)
//	var sname = null;
//	if(!id || !(sname = shares[id])) {
//		toast('无效的分享服务');
//	}
//	//验证是否授权
//	if(sname.authenticated) {
//		send(sname, ex , aid , title , body , picture);
//	} else {
//		sname.authorize(function() {
//			send(sname, ex , aid , title , body , picture);
//		}, function(e) {
//			toast('授权失败:代码' + e.code + '内容:' + e.message)
//		})
//	}
//}
//
//function send(obj, ex , aid , title , body , picture ) {
////	console.log(obj + ',' + ex + ',' + title +',' + body + ',' + picture);return; 
//	obj.send({
//		title: title,
//		extra: {
//			scene: ex
//		},
//		content: body,
//		href: apiRoot + '/Share/share/id/'+aid,
//		thumbs: [picture]
//	}, function() {
//		toast('分享成功');
//	}, function(e) {
//		console.log(JSON.stringify(e));
//		toast('您已取消分享');
//	});
//}